from rest_framework import serializers
from django.contrib.auth.models import Permission, ContentType, Group

from users.models import User


class PermissionPermsSerializer(serializers.ModelSerializer):
    """获取用户权限表列表数据"""
    # content_type = serializers.StringRelatedField(label='权限识别名')
    # content_type_id =

    class Meta:
        model = Permission
        # fields = ('id', 'name', 'codename', 'content_type')
        fields = "__all__"


# 获取权限类型列表数据
class PermissionContentTypesSerializer(serializers.ModelSerializer):
    class Meta:
        model = ContentType
        fields = ('id', 'name')


# 分组表管理
class PermissionGroupsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = "__all__"


# 用户组id
class GroupsSerializers(serializers.ModelSerializer):
    group_id = serializers.IntegerField(label='用户组ID')

    class Meta:
        model = Group
        fields = ('group_id', 'id')


# 权限id
class UserPermissionsSerializers(serializers.ModelSerializer):
    permission_id = serializers.IntegerField(label='权限ID')

    class Meta:
        model = Permission
        fields = ('permission_id', 'id')


# 管理员表管理
class PermissionAdminsSerializer(serializers.ModelSerializer):
    username = serializers.CharField(label='用户名', max_length=20, min_length=5)
    password = serializers.CharField(label='密码', max_length=20, min_length=8, write_only=True)
    # groups = GroupsSerializers(many=True, write_only=True)
    # user_permissions = UserPermissionsSerializers(many=True, write_only=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'mobile', 'password', 'groups', 'user_permissions')

    def create(self, validated_data):
        groups_data = validated_data.pop('groups')
        user_permissions_data = validated_data.pop('user_permissions')
        user = User.objects.create_superuser(**validated_data)
		
        for group in groups_data:
			# 增加
            user.groups.add(group)

        for user_permission in user_permissions_data:
			# 增加
            user.user_permissions.add(user_permission)

        return user

    def update(self, instance, validated_data):
		# TODO 验证判断
        groups_data = validated_data.pop('groups')
        user_permissions_data = validated_data.pop('user_permissions')

        super().update(instance, validated_data)

        user = User.objects.get(id=self.data.get('id'))
		
		# 查询
        groups = user.groups.all()

        for group in groups:
			# 删除
            user.groups.remove(group)
		
		# 查询
        user_permissions = user.user_permissions.all()

        for user_permission in user_permissions:
			# 删除
            user.user_permissions.remove(user_permission)

        for group_info in groups_data:
			# 增加
            user.groups.add(group_info)

        for user_permission_info in user_permissions_data:
			# 增加
            user.user_permissions.add(user_permission_info)

        return user


# 获取权限表数据
class PermissionSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Permission
        fields = ('id', 'name')


# 获取分组表数据
class PermissionGroupsSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = ('id', 'name')
